<?php

/*
*/

class Order_Detail_model extends CI_Model
{

	static $table;
	private $error;

	public function __construct()
	{
		parent::__construct();
		//载入库文件
		$this->load->library('test_database');
		$this->test_database->test();
		//载入核心类
		$this->load->database($this->test_database->get_db());
		//载入库文件
		$this->load->library('form_validation');
		//载入本地语言文件
		$this->lang->load('form_validation','chinese');
		//初始化
		$this->table = 'order_details';
		$this->error = '';
	}

	public function create($order_id)
	{
		$sub_goods_ids = $this->input->post('sub_goods_ids');
		$supplier_ids = $this->input->post('supplier_ids');
		$single_prices = $this->input->post('single_prices');
		$quantities = $this->input->post('quantities');
		$len = count($sub_goods_ids);
		$data = array();
		for ($i=0; $i<$len; $i++)
		{
			$data['order_id'] = $order_id;
			$data['sub_goods_id'] = $sub_goods_ids[$i];
			$data['supplier_id'] = $supplier_ids[$i];
			$data['price'] = $single_prices[$i];
			$data['quantity'] = $quantities[$i];
			$data['create_date'] = time();
			$this->db->insert($this->table, $data);
		}
		return;
	}

	public function get_details($order_id)
	{
		//Load Model(s)
		$this->load->model('Sub_goods_model');
		$this->load->model('Supplier_model');

		$this->db->where('order_id', $order_id);
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0)
		{
			$result = $query->result();
			//需要优化，关于 foreach SQL 操作
			$suppliers = array();
			$sub_goods = array();
			foreach ($result as $k => $data)
			{
				$temp = $this->Sub_goods_model->getOne($data->sub_goods_id);
				$result[$k]->sub_goods_number = $temp['number'];
				$result[$k]->sub_goods_attr = $temp['attribute'];
				$temp = $this->Supplier_model->getOne($data->supplier_id);
				$result[$k]->supplier_name = $temp['supplier_name'];
			}
			return $result;
		}
		else
		{
			$this->error = __FUNCTION__ . 'UNKOWN ERROR ON LINE' . __LINE__;
		}
	}

}